﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using ScrumTable.Common.Config.Xml;
using ScrumTable.DL.Data.Common.Config.Driver;

#endregion

namespace ScrumTable.DL.Data.TFS.Bulk.Config.Driver
{
    /// <summary>
    /// Represents the DriverConfig node inside the ScrumTable.DL.Data.TFS.Bulk
    /// configuration file.
    /// </summary>
    [XmlRoot(ElementName = "DriverConfig")]
    public class DriverConfig : XmlConfigSectionBase, IDriverConfig
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const string ReportingServiceConfigName = "Reporting";

        private readonly string[] _portalServiceConfigNames = new[] { "Wss", "Portal" };

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        
        /// <summary>
        /// Gets the configuration for the underlying data services.
        /// </summary>
        [XmlArray("Services")]
        [XmlArrayItem(Type = typeof(Service), ElementName = "Service")]
        public List<Service> Services { get; set; }

        [XmlIgnore]
        internal Service PortalConfig
        {
            get
            {
                return (from portalSvcNames in _portalServiceConfigNames
                        let portalService = GetService(portalSvcNames)
                        where portalService != null
                        select portalService).FirstOrDefault();
            }
        }

        [XmlIgnore]
        internal Service ReportingConfig { get { return GetService(ReportingServiceConfigName); } }

        #endregion

        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// DO NOT USE THIS CONSTRUCTOR FROM GLOBAL CODE. THE SERIALIZER NEEDS
        /// A PUBLIC CONSTRUCTOR!
        /// </summary>
        public DriverConfig()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        private Service GetService(string serviceName)
        {
            Service result = null;

            if (Services != null)
            {
                result = (from svc in Services
                          where svc.Name == serviceName
                          select svc).FirstOrDefault();
            }

            if (result == null)
            {
                // service configuration not found, create default config
                result = new Service { Enabled = true, Name = serviceName };
            }
            return result;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
